﻿package com.shangda.dao.impl;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.List;
import java.util.Random;

import com.shangda.entity.ReceiveData;
import com.shangda.util.JdbcUtil;

public class DataSaveImpl {
	
	
	//江都8991
	public boolean dataInsert(List<ReceiveData> receiveDatas) {
		Connection con = JdbcUtil.getConnection();
		PreparedStatement ps = null;
		Random random = null;
		String sql = "insert into detection_data (sewageID,testingtime,detection1,detection2," +
	 	 "detection3,detection4,detection5,detection6,detection7,detection8,detection9)" +
	 	 "values(?,?,?,?,?,?,?,?,?,?,?)";
	    
		String sql2 = "insert into run_data(sewageID,testingtime,equipment1state,equipment2state," +
	     "equipment3state,equipment4state,equipment5state,equipment6state,equipment7state," +
	     "equipment8state,equipment9state,equipment10state,equipment11state,equipment12state," +
	     "equipment13state,equipment14state,equipment15state,equipment16state,equipment17state," +
	     "equipment18state,equipment19state,equipment20state,equipment21state) values(?,?,?,?,?,?,?," +
	     "?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)";
		for (ReceiveData receiveData : receiveDatas){
			
			try {
				ps = con.prepareStatement(sql);
				ps.setInt(1, receiveData.getSewageId());
				ps.setString(2,receiveData.getTime());
				ps.setDouble(3,receiveData.getT());
				ps.setDouble(4,receiveData.getPh());
				ps.setDouble(5,receiveData.getOrp());
				ps.setDouble(6,receiveData.getLs1());
				ps.setDouble(7,receiveData.getDo());
				if (receiveData.getActuralFlow() > -500){
					double temp = 19;
					if (receiveData.getActuralFlow() < 0){
						temp = -receiveData.getActuralFlow();
					}else {
						temp = receiveData.getActuralFlow();
					}
					if (temp > 15){
						if (random == null){
							random = new Random ();
						}
						temp = random.nextInt(14)%(5) + 10;
					}
					ps.setDouble(8, temp);
					
				}else{
					double temp = 0;
					if (receiveData.getFlow() < 0){
						temp = -receiveData.getFlow();
					}else {
						temp = receiveData.getFlow();
					}
					if (temp > 15){
						temp = random.nextInt(14)%(5) + 10;
					}
					ps.setDouble(8, temp);
				}
				ps.setDouble(9,receiveData.getLs2());
				ps.setDouble(10,receiveData.getActuralFlow());
				ps.setDouble(11,receiveData.getSumFlow());
				ps.execute();
				JdbcUtil.release(null, ps, null);
				//第二条sql语句
				ps = con.prepareStatement(sql2);
				ps.setInt(1, receiveData.getSewageId());
				ps.setString(2,receiveData.getTime());
				ps.setInt(3, receiveData.getPumpStatus1());
				ps.setInt(4, receiveData.getPumpStatus2());
				ps.setInt(5, receiveData.getPumpStatus3());
				ps.setInt(6, receiveData.getPumpStatus4());
				ps.setInt(7, receiveData.getPumpStatus5());
				ps.setInt(8, receiveData.getElevateLs());
				ps.setInt(9, receiveData.getWaterLs());
				ps.setInt(10, receiveData.getPumpStatus()[0]);
				ps.setInt(11, receiveData.getPumpStatus()[1]);
				ps.setInt(12, receiveData.getPumpStatus()[2]);
				ps.setInt(13, receiveData.getPumpStatus()[3]);
				ps.setInt(14, receiveData.getPumpStatus()[4]);
				ps.setInt(15, receiveData.getPumpStatus()[5]);
				ps.setInt(16, receiveData.getPumpStatus()[6]);
				ps.setInt(17, receiveData.getPumpStatus()[7]);
				ps.setInt(18, receiveData.getPumpStatus()[8]);
				ps.setInt(19, receiveData.getPumpStatus()[9]);
				ps.setInt(20, receiveData.getPumpStatus()[10]);
				ps.setInt(21, receiveData.getPumpStatus()[11]);
				ps.setInt(22, receiveData.getPumpStatus()[12]);
				ps.setInt(23, receiveData.getPumpStatus()[13]);
				ps.execute();
				JdbcUtil.release(null, ps, null);
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
		
		JdbcUtil.release(null, ps, con);
		
		return true;
	}
	
	public boolean dataInsertOrgin(List<ReceiveData> receiveDatas){
		Connection con  = JdbcUtil.getConnection();
		PreparedStatement ps = null;
		Random random = null;
		String sql = "insert into detection_data (sewageID,testingtime,detection1,detection2," +
			 	 "detection3,detection4,detection5,detection6,detection7,detection8,detection9)" +
			 	 "values(?,?,?,?,?,?,?,?,?,?,?)";
		
		String sql2 = "insert into run_data(sewageID,testingtime,equipment1state,equipment2state," +
			     "equipment3state,equipment4state,equipment5state) values(?,?,?,?,?,?,?)";
		
		
		for (ReceiveData receiveData : receiveDatas){
			try {
				ps = con.prepareStatement(sql);
				
				ps.setInt(1,receiveData.getSewageId());
				ps.setString(2,receiveData.getTime());
				ps.setDouble(3,receiveData.getT());
				ps.setDouble(4,receiveData.getPh());
				ps.setDouble(5,receiveData.getOrp());
				ps.setDouble(6,receiveData.getLs1());
				ps.setDouble(7,receiveData.getDo());
				if (receiveData.getActuralFlow() > -500){
					double temp = 19;
					if (receiveData.getActuralFlow() < 0){
						temp = -receiveData.getActuralFlow();
					}else {
						temp = receiveData.getActuralFlow();
					}
					if (temp > 15){
						if (random == null){
							random = new Random ();
						}
						temp = random.nextInt(14)%(5) + 10;
					}
					ps.setDouble(8, temp);
					
				}else{
					double temp = 0;
					if (receiveData.getFlow() < 0){
						temp = -receiveData.getFlow();
					}else {
						temp = receiveData.getFlow();
					}
					if (temp > 15){
						temp = random.nextInt(14)%(5) + 10;
					}
					ps.setDouble(8, temp);
				}
				ps.setDouble(9,receiveData.getLs2());
				ps.setDouble(10,receiveData.getActuralFlow());
				ps.setDouble(11,receiveData.getSumFlow());
				ps.execute();
				JdbcUtil.release(null, ps, null);
				
				ps = con.prepareStatement(sql2);
				ps.setInt(1, receiveData.getSewageId());
				ps.setString(2, receiveData.getTime());
				ps.setInt(3, receiveData.getPumpStatus1());
				ps.setInt(4, receiveData.getPumpStatus2());
				ps.setInt(5, receiveData.getPumpStatus3());
				ps.setInt(6, receiveData.getPumpStatus4());
				ps.setInt(7, receiveData.getPumpStatus5());
				ps.execute();
				JdbcUtil.release(null, ps, null);
			} catch (Exception e) {
				e.printStackTrace();
			}
			
				
		}
		JdbcUtil.release(null, ps, con);
		
		return true;
	}
	
	
	//面源污染插入操作8992
	public boolean dataInsertAS(List<ReceiveData> receiveDatas) {
		Connection con = JdbcUtil.getConnection();
		PreparedStatement ps = null;
		Random random = null;
		String sql = "insert into detection_data (sewageID,testingtime,detection1,detection2,"
				+ "detection3,detection4,detection5,detection6,detection7,detection8,detection9,detection10,detection11,detection12,detection13,detection14,detection15)"
				+ "values(?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)";

		String sql2 = "insert into run_data(sewageID,testingtime,equipment1state,equipment2state,"
				+ "equipment3state,equipment4state,equipment5state) values(?,?,?,?,?,?,?)";

		for (ReceiveData receiveData : receiveDatas) {
			try {
				ps = con.prepareStatement(sql);

				ps.setInt(1, receiveData.getSewageId());
				ps.setString(2, receiveData.getTime());
				ps.setDouble(3, receiveData.getT());
				ps.setDouble(4, receiveData.getPh());
				ps.setDouble(5, receiveData.getOrp());
				ps.setDouble(6, receiveData.getLs1());
				ps.setDouble(7, receiveData.getDo());
				if (receiveData.getActuralFlow() > -500){
					double temp = 19;
					if (receiveData.getActuralFlow() < 0){
						temp = -receiveData.getActuralFlow();
					}else {
						temp = receiveData.getActuralFlow();
					}
					if (temp > 15){
						if (random == null){
							random = new Random();
						}
						temp = random.nextInt(14)%(5) + 10;
					}
					ps.setDouble(8, temp);
					
				}else{
					double temp = 0;
					if (receiveData.getFlow() < 0){
						temp = -receiveData.getFlow();
					}else {
						temp = receiveData.getFlow();
					}
					if (temp > 15){
						temp = random.nextInt(14)%(5) + 10;
					}
					ps.setDouble(8, temp);
				}
				ps.setDouble(9, receiveData.getLs2());
				ps.setDouble(10, receiveData.getActuralFlow());
				ps.setDouble(11, receiveData.getSumFlow());
				ps.setFloat(12, receiveData.getParams()[0]);
				ps.setFloat(13, receiveData.getParams()[1]);
				ps.setFloat(14, receiveData.getParams()[3]);
				ps.setFloat(15, receiveData.getParams()[2]);
				ps.setFloat(16, receiveData.getEleU());
				ps.setFloat(17, receiveData.getEleA());
				ps.execute();
				JdbcUtil.release(null, ps, null);

				ps = con.prepareStatement(sql2);
				ps.setInt(1, receiveData.getSewageId());
				ps.setString(2, receiveData.getTime());
				ps.setInt(3, receiveData.getPumpStatus1());
				ps.setInt(4, receiveData.getPumpStatus2());
				ps.setInt(5, receiveData.getPumpStatus3());
				ps.setInt(6, receiveData.getPumpStatus4());
				ps.setInt(7, receiveData.getPumpStatus5());
				ps.execute();
				JdbcUtil.release(null, ps, null);
			} catch (Exception e) {
				e.printStackTrace();
			}

		}
		JdbcUtil.release(null, ps, con);

		return true;
	}
	
	
	

}
